package GUI;

import javax.swing.JFrame;

import entities.*;
import voting.ClientVoting;

import network.Client;
//zm9rfXpvWOQ=     salt in reg  WBgGLba/CpfWTu0DA3Virv08ZdI=
@SuppressWarnings("serial")
public class MainFrame extends JFrame{
	private static MainFrame mainFrame;
	private LoginPanel loginPanel;
	RegisterPanel regPanel;
	ActivatePanel activatePanel;
	boolean isAdmin;
	public String serverPublicKeyMod ;
	public String serverPublicKeyExp ;
	public MainFrame(boolean isAdmin){
		serverPublicKeyExp = null;
		serverPublicKeyMod = null;
		initializeFrame();
		if(isAdmin)
		{
			AdminLoginPanel adminLoginPnl = new AdminLoginPanel();
			this.add(adminLoginPnl);
		}
		else{
			regPanel = new RegisterPanel();
			activatePanel = new ActivatePanel();
			loginPanel = new LoginPanel();
			
			this.add(loginPanel);	
		}
		
		this.setVisible(true);
	}
	
	public void addLoginPanel(){
		this.add(loginPanel);
		this.repaint();
		this.validate();
	}
	
	private void initializeFrame(){
		this.setBounds(100,100,750,650);
		this.setTitle("E-Voting System");
		this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		this.setVisible(true);
	}


	public static MainFrame getMainFrame() {
		return mainFrame;
	}

	public void addRegisterationPnl() {
		regPanel = new RegisterPanel();
		this.add(regPanel);
		this.repaint();
		this.validate();
		
	}
	
	public void addActivatePnl() {
		ActivatePanel activatePnl = new ActivatePanel();
		this.add(activatePnl );
		this.repaint();
		this.validate();
	}
	
	public void addViewVotingPnl(boolean isAdmin) {
		System.out.println("adding viewing panel");
		ViewVotingsPanel votingsPnl = new ViewVotingsPanel(isAdmin);
		this.add(votingsPnl);
		this.repaint();
		this.validate();
	}
	public void addVotingResultsPnl(Voting voting,boolean isAdmin)
	{
		VotingResultsPanel votingResultsPnl=new VotingResultsPanel(voting,isAdmin);
		this.add(votingResultsPnl);
		this.repaint();
		this.validate();
		
	}
	public void addCreateVotingPnl()
	{
		CreateVotingPanel createVotingPnl=new CreateVotingPanel();
		this.add(createVotingPnl);
		this.repaint();
		this.validate();
	}
	public void addVotingPanel(Voting v)
	{
		Candidate [] candidates = ClientVoting.getCandidates(v);
		VotingPanel p = new VotingPanel(v,candidates);
		add(p);
		repaint();
		validate();
	}
	public void addViewVotesPanel(Voting v,boolean isAdmin)
	{
		String [] votes = ClientVoting.getAllVotes(-1, v.getVotingID());
		ViewVotesPanel vvp = new ViewVotesPanel(votes,isAdmin);
		add(vvp);
		repaint();
		validate();
	}
	
	public static void run(boolean isAdmin) {
		Client.initializeClient();
		mainFrame = new MainFrame(isAdmin);   
	}	
	
	public static void main(String[] args) {
		Client.initializeClient();
		mainFrame = new MainFrame(false);   //for client
		//mainFrame = new MainFrame(true);	//for admin
	}	
	
}
